<template>
  <div class="register-container">
    <div class="register-header">
      <el-icon class="back-btn" :size="24" @click="goBack">
        <ArrowLeft />
      </el-icon>
      <h1 class="register-title">注册</h1>
    </div>
    
    <div class="register-content">
      <div class="register-logo">
        <el-icon :size="70">
          <VideoPlay />
        </el-icon>
        <h2>加入我的抖音</h2>
        <p>开始记录美好生活</p>
      </div>
      
      <el-form
        ref="registerFormRef"
        :model="registerForm"
        :rules="registerRules"
        class="register-form"
        size="large"
      >
        <el-form-item prop="username">
          <el-input
            v-model="registerForm.username"
            placeholder="用户名 (3-20位)"
            prefix-icon="User"
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="nickname">
          <el-input
            v-model="registerForm.nickname"
            placeholder="昵称 (最多20位)"
            prefix-icon="Avatar"
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="email">
          <el-input
            v-model="registerForm.email"
            placeholder="邮箱 (选填)"
            prefix-icon="Message"
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="phone">
          <el-input
            v-model="registerForm.phone"
            placeholder="手机号 (选填)"
            prefix-icon="Phone"
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="registerForm.password"
            type="password"
            placeholder="密码 (6-20位)"
            prefix-icon="Lock"
            show-password
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="confirmPassword">
          <el-input
            v-model="registerForm.confirmPassword"
            type="password"
            placeholder="确认密码"
            prefix-icon="Lock"
            show-password
            clearable
            @keyup.enter="handleRegister"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            class="register-btn"
            :loading="loading"
            @click="handleRegister"
          >
            {{ loading ? '注册中...' : '注册' }}
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="register-footer">
        <span>已有账号？</span>
        <router-link to="/login" class="login-link">
          立即登录
        </router-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { register } from '@/api/user'
import { ElMessage } from 'element-plus'
import { ArrowLeft, VideoPlay } from '@element-plus/icons-vue'

const router = useRouter()

const registerFormRef = ref(null)
const loading = ref(false)

// 注册表单数据
const registerForm = reactive({
  username: '',
  nickname: '',
  email: '',
  phone: '',
  password: '',
  confirmPassword: ''
})

// 自定义验证器
const validateConfirmPassword = (rule, value, callback) => {
  if (value !== registerForm.password) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}

const validateEmail = (rule, value, callback) => {
  if (value && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)) {
    callback(new Error('请输入正确的邮箱格式'))
  } else {
    callback()
  }
}

const validatePhone = (rule, value, callback) => {
  if (value && !/^1[3-9]\d{9}$/.test(value)) {
    callback(new Error('请输入正确的手机号格式'))
  } else {
    callback()
  }
}

// 表单验证规则
const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度为3-20位', trigger: 'blur' },
    { pattern: /^[a-zA-Z0-9_]+$/, message: '用户名只能包含字母、数字和下划线', trigger: 'blur' }
  ],
  nickname: [
    { required: true, message: '请输入昵称', trigger: 'blur' },
    { max: 20, message: '昵称最多20位', trigger: 'blur' }
  ],
  email: [
    { validator: validateEmail, trigger: 'blur' }
  ],
  phone: [
    { validator: validatePhone, trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度为6-20位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: validateConfirmPassword, trigger: 'blur' }
  ]
}

// 处理注册
const handleRegister = async () => {
  if (!registerFormRef.value) return
  
  try {
    await registerFormRef.value.validate()
  } catch (error) {
    return
  }
  
  loading.value = true
  
  try {
    await register(registerForm)
    
    ElMessage.success('注册成功，请登录')
    
    // 注册成功后跳转到登录页
    router.push('/login')
    
  } catch (error) {
    console.error('注册失败:', error)
    // 错误信息已在请求拦截器中处理
  } finally {
    loading.value = false
  }
}

// 返回上一页
const goBack = () => {
  if (window.history.length > 1) {
    router.go(-1)
  } else {
    router.push('/')
  }
}
</script>

<style lang="scss" scoped>
.register-container {
  width: 100vw;
  min-height: 100vh;
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: auto;
  
  // 背景装饰
  &::before {
    content: '';
    position: fixed;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: radial-gradient(circle, rgba(255, 255, 255, 0.08) 1px, transparent 1px);
    background-size: 60px 60px;
    animation: float 25s ease-in-out infinite;
    z-index: 1;
  }
}

.register-header {
  position: relative;
  z-index: 10;
  display: flex;
  align-items: center;
  padding: 20px;
  
  .back-btn {
    color: $text-white;
    cursor: pointer;
    transition: $transition-fast;
    
    &:hover {
      transform: translateX(-2px);
    }
  }
  
  .register-title {
    flex: 1;
    text-align: center;
    color: $text-white;
    font-size: 20px;
    font-weight: 600;
    margin: 0;
  }
}

.register-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 20px 40px 40px;
  position: relative;
  z-index: 10;
}

.register-logo {
  text-align: center;
  margin-bottom: 30px;
  
  .el-icon {
    color: $text-white;
    margin-bottom: 12px;
  }
  
  h2 {
    color: $text-white;
    font-size: 24px;
    font-weight: 700;
    margin: 0 0 6px 0;
  }
  
  p {
    color: rgba(255, 255, 255, 0.8);
    font-size: 14px;
    margin: 0;
  }
}

.register-form {
  background: rgba(255, 255, 255, 0.95);
  border-radius: $border-radius-large;
  padding: 24px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  max-width: 400px;
  margin: 0 auto;
  width: 100%;
  
  .el-form-item {
    margin-bottom: 20px;
    
    :deep(.el-input__wrapper) {
      border-radius: $border-radius;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
      transition: $transition-fast;
      
      &:hover {
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
      }
    }
    
    :deep(.el-input.is-focus .el-input__wrapper) {
      box-shadow: 0 0 0 2px rgba(240, 147, 251, 0.3);
    }
  }
  
  .register-btn {
    width: 100%;
    height: 48px;
    font-size: 16px;
    font-weight: 600;
    border-radius: $border-radius;
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
    border: none;
    transition: $transition-fast;
    
    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 8px 20px rgba(240, 147, 251, 0.4);
    }
    
    &:active {
      transform: translateY(0);
    }
  }
}

.register-footer {
  text-align: center;
  margin-top: 24px;
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
  
  .login-link {
    color: $text-white;
    font-weight: 600;
    text-decoration: none;
    margin-left: 6px;
    transition: $transition-fast;
    
    &:hover {
      color: rgba(255, 255, 255, 0.8);
      text-decoration: underline;
    }
  }
}

@keyframes float {
  0%, 100% {
    transform: translate(0, 0) rotate(0deg);
  }
  25% {
    transform: translate(-0.5%, -0.5%) rotate(0.5deg);
  }
  50% {
    transform: translate(0.5%, 0.5%) rotate(-0.5deg);
  }
  75% {
    transform: translate(-0.5%, 0.5%) rotate(0.5deg);
  }
}

// 响应式设计
@media (max-width: 480px) {
  .register-content {
    padding: 15px 20px 30px;
  }
  
  .register-form {
    padding: 20px;
  }
  
  .register-logo {
    margin-bottom: 24px;
    
    .el-icon {
      font-size: 56px;
    }
    
    h2 {
      font-size: 20px;
    }
    
    p {
      font-size: 13px;
    }
  }
  
  .register-form .el-form-item {
    margin-bottom: 16px;
  }
}
</style> 